<?php

/**
 * CompraInternacional form.
 *
 * @package    dirsac
 * @subpackage form
 * @author     Miguel Flores
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class CompraInternacionalForm extends BaseCompraInternacionalForm
{
  public function configure()
  { 
  	$this->validatorSchema['correo_encargado'] = new sfValidatorEmail();
  	$this->validatorSchema['correo_contacto'] = new sfValidatorEmail();
  	
  	
	unset($this['created_at'], $this['updated_at']);
    $this->embedRelation('ItemCompraInternacional');
    
    $this->widgetSchema['observacion'] = new sfWidgetFormInputText();
    
    $this->widgetSchema['estado'] = new sfWidgetFormInputText();
    $this->widgetSchema['estado']->setAttribute('readonly', 'true');
    
    $this->widgetSchema['factura'] = new sfWidgetFormInputFileEditable(array(
      'label'     => 'Factura',
      'file_src'  => '/uploads/facturas/'.$this->getObject()->getFactura(),
      'is_image'  => false,
      'edit_mode' => !$this->isNew(),
      'template'  => '<div><a href="%file%">ver factura</a><br /><label></label>%input%<br /><label></label>%delete% Eliminar archivo actual</div>',
    ));
    
    $this->validatorSchema['factura'] = new sfValidatorFile(array(
      'required' => false,
      'path'     => sfConfig::get('sf_upload_dir').'/facturas',
      'max_size' => '20971520'
    ));
    
    $this->widgetSchema['packing'] = new sfWidgetFormInputFileEditable(array(
      'label'     => 'Packing',
      'file_src'  => '/uploads/packing/'.$this->getObject()->getPacking(),
      'is_image'  => false,
      'edit_mode' => !$this->isNew(),
      'template'  => '<div><a href="%file%">ver packing</a><br /><label></label>%input%<br /><label></label>%delete% Eliminar archivo actual</div>',
    ));
    
    $this->validatorSchema['packing'] = new sfValidatorFile(array(
      'required' => false,
      'path'     => sfConfig::get('sf_upload_dir').'/packing',
      'max_size' => '20971520'
    ));
    
    $this->widgetSchema['cotizacion'] = new sfWidgetFormInputFileEditable(array(
      'label'     => 'Cotización',
      'file_src'  => '/uploads/cotizacion/'.$this->getObject()->getCotizacion(),
      'is_image'  => false,
      'edit_mode' => !$this->isNew(),
      'template'  => '<div><a href="%file%">ver factura</a><br /><label></label>%input%<br /><label></label>%delete% Eliminar archivo actual</div>',
    ));
    
    $this->validatorSchema['cotizacion'] = new sfValidatorFile(array(
      'required' => false,
      'path'     => sfConfig::get('sf_upload_dir').'/cotizacion',
      'max_size' => '20971520'
    ));
    
    $this->widgetSchema['otros'] = new sfWidgetFormInputFileEditable(array(
      'label'     => 'Otros',
      'file_src'  => '/uploads/otros/'.$this->getObject()->getOtros(),
      'is_image'  => false,
      'edit_mode' => !$this->isNew(),
      'template'  => '<div><a href="%file%">ver packing</a><br /><label></label>%input%<br /><label></label>%delete% Eliminar archivo actual</div>',
    ));
    
    $this->validatorSchema['otros'] = new sfValidatorFile(array(
      'required' => false,
      'path'     => sfConfig::get('sf_upload_dir').'/otros',
      'max_size' => '20971520'
    ));
    
    $this->widgetSchema['total'] = new sfWidgetFormInputText();
    $this->widgetSchema['total']->setAttribute('id', 'total');
    
    $this->widgetSchema['plazo_entrega'] = new sfWidgetFormInputText(array());
    $this->widgetSchema['plazo_entrega']->setAttribute('class', 'datepicker');
    
    
    $this->widgetSchema['cuenta_id'] = new sfWidgetFormDoctrineChoice(array(
    	'model' => $this->getRelatedModelName('Cuenta'), 
    	//'table_method' => 'getCuentasAsociadas',
    	'add_empty' => false
    ));
    
  }

  public function addNewFields($number){
    $new_items = new BaseForm();

    for($i=0; $i <= $number; $i+=1){
      $item = new ItemCompraInternacional();
      $item->setCompraInternacional($this->getObject());
  
      $item_form = new ItemCompraInternacionalForm($item);
      
      $new_items->embedForm($i,$item_form);
    }
    $this->embedForm('new', $new_items);
  }

  public function bind(array $taintedValues = null, array $taintedFiles = null){
    $new_items = new BaseForm();
    
    if (isset($taintedValues['new'])) {
      foreach($taintedValues['new'] as $key => $new_item){
        $item = new ItemCompraInternacional();
        $item->setCompraInternacional($this->getObject());
      
        $item_form = new ItemCompraInternacionalForm($item);

        $new_items->embedForm($key,$item_form);
      }
    }
    $this->embedForm('new',$new_items);

    parent::bind($taintedValues, $taintedFiles);
  }

}
